package com.ztgf.commodity.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.commodity.models.bo.CommodityAdvisoryPriceFileSaveBO;
import com.ztgf.commodity.models.dto.CommodityAdvisoryPriceFileDTO;
import com.ztgf.commodity.models.service.ICommodityAdvisoryPriceFileService;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
 * @author: chenyu.wang
 * @date: 2020-08-11
 * @description:询价模板表控制器
 */
@RestController
@RequestMapping("/cms/commodityAdvisoryPriceFileController")
@Api(value = "CommodityAdvisoryPriceFileController", tags = "询价模板表接口")
public class CommodityAdvisoryPriceFileController {

    @DubboReference
    private ICommodityAdvisoryPriceFileService commodityAdvisoryPriceFileService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加询价表", notes = "添加询价表")
    public Result insert(CommodityAdvisoryPriceFileSaveBO saveBO) {
        if (!StringUtils.isNotBlank(saveBO.getFileUrl())) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = commodityAdvisoryPriceFileService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping(value = "/selectFile")
    @ApiOperation(value = "查询询价模板文件", notes = "查询询价模板文件")
    public Result selectFile() {
        Wrapper<CommodityAdvisoryPriceFileDTO> wrapper = new EntityWrapper<>();
        return new Result<>(commodityAdvisoryPriceFileService.selectOne(wrapper));
    }

    @PostMapping(value = "/updateUrl")
    @ApiOperation(value = "更新模板地址", notes = "更新模板地址")
    public Result updateUrl(String url, Integer type, String filename) {
        if (StringUtils.isEmpty(url) || StringUtils.isEmpty(filename) || type == null){
            return new Result<>(CodeEnum.FAIL_100002);
        }

        boolean flag = commodityAdvisoryPriceFileService.updateUrl(url, type, filename);
        if (flag){
            return new Result<>();
        }else{
            return new Result<>(CodeEnum.FAIL);
        }
    }

    @PostMapping(value = "/queryList")
    @ApiOperation(value = "模板列表", notes = "模板列表")
    public Result queryList() {
        Wrapper<CommodityAdvisoryPriceFileDTO> wrapper = new EntityWrapper<>();
        List<CommodityAdvisoryPriceFileDTO> dtos = commodityAdvisoryPriceFileService.selectList(wrapper);
        return new Result<>(dtos);
    }

    @PostMapping(value = "/selectFileByType")
    @ApiOperation(value = "根据类型查询模板文件", notes = "根据类型查询模板文件")
    public Result selectFileByType(Integer type) {
        Wrapper<CommodityAdvisoryPriceFileDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);
        return new Result<>(commodityAdvisoryPriceFileService.selectOne(wrapper));
    }
}
